package com.ruoyi.web.controller.equipment;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseShop;
import com.ruoyi.system.domain.equipment.EquipmentType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IEquipmentTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备类型Controller
 *
 * @author ruoyi
 * @date 2023-02-27
 */
@Api(tags = "设备类型api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/equipment/type" )
public class EquipmentTypeController extends BaseController {

    private final IEquipmentTypeService iEquipmentTypeService;

    /**
     * 查询设备类型列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EquipmentType equipmentType)
    {
        startPage();
        LambdaQueryWrapper<EquipmentType> lqw = new LambdaQueryWrapper<EquipmentType>();
        if (StringUtils.isNotBlank(equipmentType.getTypeName())){
            lqw.like(EquipmentType::getTypeName ,equipmentType.getTypeName());
        }
        if (StringUtils.isNotBlank(equipmentType.getTypeCode())){
            lqw.eq(EquipmentType::getTypeCode ,equipmentType.getTypeCode());
        }
        List<EquipmentType> list = iEquipmentTypeService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出设备类型列表
     */
    @Log(title = "设备类型" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(EquipmentType equipmentType) {
        LambdaQueryWrapper<EquipmentType> lqw = new LambdaQueryWrapper<EquipmentType>(equipmentType);
        List<EquipmentType> list = iEquipmentTypeService.list(lqw);
        ExcelUtil<EquipmentType> util = new ExcelUtil<EquipmentType>(EquipmentType. class);
        return util.exportExcel(list, "type" );
    }

    /**
     * 获取设备类型详细信息
     */
    @ApiOperation(value = "获取设备类型详细信息")
    @GetMapping(value = "/{typeId}" )
    public AjaxResult getInfo(@PathVariable("typeId" ) Long typeId) {
        return AjaxResult.success(iEquipmentTypeService.getById(typeId));
    }

    /**
     * 新增设备类型
     */
    @Log(title = "新增设备类型", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增设备类型")
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody EquipmentType equipmentType)
    {
        if (UserConstants.NOT_UNIQUE.equals(iEquipmentTypeService.checkDeptNameUnique(equipmentType)))
        {
            return AjaxResult.error("新增设备类型'" + equipmentType.getTypeName() + "'失败，设备类型名称已存在");
        }
        equipmentType.setCreateBy(SecurityUtils.getUsername());
        equipmentType.setCreateTime(new Date());
        return toAjax(iEquipmentTypeService.insertShop(equipmentType));
    }

    /**
     * 修改设备类型
     */
    @ApiOperation(value = "修改设备类型")
    @Log(title = "设备类型" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentType equipmentType) {
        equipmentType.setCreateBy(SecurityUtils.getUsername());
        equipmentType.setCreateTime(new Date());
        return toAjax(iEquipmentTypeService.updateById(equipmentType) ? 1 : 0);
    }

    /**
     * 删除设备类型
     */
    @ApiOperation(value = "删除设备类型")
    @Log(title = "设备类型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeId}" )
    public AjaxResult remove(@PathVariable Long typeId) {
        LambdaQueryWrapper<EquipmentType> lqw = new LambdaQueryWrapper<EquipmentType>();
        if (null!=typeId){
            lqw.like(EquipmentType::getParentId ,typeId);
        }
        List<EquipmentType> list = iEquipmentTypeService.list(lqw);
        if (IterUtil.isNotEmpty(list))
        {
            return AjaxResult.error("存在下级,不允许删除");
        }
        return toAjax(iEquipmentTypeService.removeById(typeId) ? 1 : 0);
    }


    /**
     * 获取设备类型下拉树列表
     */
    @ApiOperation(value = "获取设备类型下拉树列表")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(EquipmentType equipmentType)
    {
        List<EquipmentType> list = iEquipmentTypeService.selectShopList(equipmentType);
        return AjaxResult.success(iEquipmentTypeService.buildShopTreeSelect(list));
    }


}
